//
//  UpdatePassWordViewController.m
//  FingerUniversity
//
//  Created by vantle on 16/3/12.
//  Copyright © 2016年 vantle. All rights reserved.
//

#import "UpdatePassWordViewController.h"
#import "AFNetworking.h"
#import "MyMD5.h"
#import "SVProgressHUD.h"
#import "UIColor+hexColor.h"
#import "MobClick.h"
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
@interface UpdatePassWordViewController ()

@end

@implementation UpdatePassWordViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [MobClick beginLogPageView:@"UpdatePassWordViewController"];//("PageOne"为页面名称，可自定义)
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"UpdatePassWordViewController"];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    
    UIImageView*bg=[[UIImageView alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height)];
    bg.image=[UIImage imageNamed:@"bg"];
    bg.userInteractionEnabled=YES;
    [self.view addSubview:bg];
    
    UIImageView*barImage=[[UIImageView alloc]initWithFrame:CGRectMake(0, 20, ScreenWidth, 44)];
    barImage.userInteractionEnabled=YES;
    barImage.backgroundColor=[UIColor colorWithRed:0.92 green:0.92 blue:0.92 alpha:1];
    [self.view addSubview:barImage];
    
    UIButton*backButton=[UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame=CGRectMake(0, 0, 100, 44);
    [backButton addTarget:self action:@selector(backButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [barImage addSubview:backButton];
    
    UIImageView*backImage=[[UIImageView alloc]initWithFrame:CGRectMake(5, 12, 5, 20)];
    backImage.image=[UIImage imageNamed:@"back_arrowleft"];
    [barImage addSubview:backImage];
    
    UIImageView*logoImage=[[UIImageView alloc]initWithFrame:CGRectMake(backImage.frame.origin.x+backImage.frame.size.width+3, 6, 32, 32)];
    logoImage.layer.cornerRadius=3;
    logoImage.clipsToBounds=YES;
    logoImage.image=[UIImage imageNamed:@"app-logo-80"];
    [barImage addSubview:logoImage];
    
    UILabel*noticeLabel=[[UILabel alloc]initWithFrame:CGRectMake(logoImage.frame.origin.x+logoImage.frame.size.width+5, 10, 100, 25)];
    noticeLabel.text=@"密码修改";
    [barImage addSubview:noticeLabel];
    
    
    
    
    UIImageView*loginImage=[[UIImageView alloc]init];
    loginImage.center=CGPointMake(ScreenWidth/2, 225);
    loginImage.bounds=CGRectMake(0, 0, ScreenWidth-54, 100);
    loginImage.backgroundColor=[UIColor hexFloatColor:@"eef3f4"];
    loginImage.layer.cornerRadius=6;
    loginImage.clipsToBounds=YES;
    [self.view addSubview:loginImage];
    
    UIImageView*lineView=[[UIImageView alloc]init];
    lineView.backgroundColor=[UIColor hexFloatColor:@"d9d9d9"];
    lineView.center=CGPointMake(ScreenWidth/2, 225);
    lineView.bounds=CGRectMake(0, 0, ScreenWidth-64, 1);
    [self.view addSubview:lineView];
    
    _oldPassWord=[[UITextField alloc]init];
    _oldPassWord.center=CGPointMake(ScreenWidth/2, 200);
    _oldPassWord.placeholder=@"  请输入旧密码";
    _oldPassWord.delegate=self;
    _oldPassWord.textColor=[UIColor blackColor];
    _oldPassWord.bounds=CGRectMake(0, 0, ScreenWidth-60, 50);
    [self.view addSubview:_oldPassWord];
    
    _passWord=[[UITextField alloc]init];
    _passWord.center=CGPointMake(ScreenWidth/2, 250);
    _passWord.delegate=self;
    _passWord.textColor=[UIColor blackColor];
    _passWord.placeholder=@"  请输入新密码";
    _passWord.bounds=CGRectMake(0,0,ScreenWidth-60, 50);
    [self.view addSubview:_passWord];
    
    UIButton*button=[UIButton buttonWithType: UIButtonTypeCustom];
    button.frame=CGRectMake(30, _passWord.frame.origin.y+_passWord.frame.size.height+15, ScreenWidth-60, 50);
    button.layer.cornerRadius=6;
    button.clipsToBounds=YES;
    button.tag=100;
    button.titleLabel.font=[UIFont systemFontOfSize:20];
    [button setBackgroundImage:[UIImage imageNamed:@"圆角矩形-2"] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor hexFloatColor:@"ffffff"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(updateButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"密码修改" forState:UIControlStateNormal];
    [bg addSubview:button];
    

    
}
-(void)upDatePassWord
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString*userId=[userDefaults objectForKey:@"userId"];
    NSString*flag=[userDefaults objectForKey:@"flag"];
    NSString*passWord=[MyMD5 md5:_oldPassWord.text];
    NSString*newPassWord=[MyMD5 md5:_passWord.text];
    NSLog(@"-------%@,%@,%@,%@",userId,flag,passWord,newPassWord);
    
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes =[manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
    
    NSDictionary*params=@{@"userId":userId,@"passWord":passWord,@"passWordChange":newPassWord,@"flag":flag};

    
    [manager POST:@"http://120.76.79.85:9080/mzdx/service/changePassWord.action" parameters:params success: ^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData*responseData=responseObject;
        NSString*str=[[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseData
                                                            options:NSJSONReadingMutableContainers
                                                              error:nil];
        NSLog(@"🐱＝＝＝＝%@",str);
        NSLog(@"🐶＝＝＝＝＝%@",responseObject);
        NSString*rType=[dic objectForKey:@"rType"];
        if ([rType isEqualToString:@"1"]) {
            [self.navigationController popViewControllerAnimated:YES];
            [SVProgressHUD showImage:nil status:@"密码更新成功"];
        }
        else
        {
            [SVProgressHUD showImage:nil status:@"密码错误"];
        }
    } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"%@",error);
    }];
}
-(void)updateButtonClick
{
    if (_oldPassWord.text.length==0) {
        [SVProgressHUD showImage:nil status:@"请输入旧密码"];
    }
    else if (_passWord.text.length==0)
    {
        [SVProgressHUD showImage:nil status:@"请输入新密码"];
    }
    else
    {
        [self upDatePassWord];
    }
    
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.view endEditing:YES];
    return YES;
}

-(void)backButtonClick
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
